function sendRequest (promises, max, callback) {
  let count = 0
  let len = promises.length
  let results = []
  const next = () => {
    while (count < max && len > 0) {
      console.log(count)
      const index = promises.length - len
      len--
      results[index] = {
        value: null,
        reason: null
      }
      Promise.resolve(promises[index]).then(res => {
        results[index].value = res
      }, reason => {
        results[index].reason = reason
      }).finally(() => {
        count--
        next()
      })
      count++
    }
    if (count == 0) {
      callback(results)
    }
  }
  next()
}

function text (num) {
  // let num = Math.random()
  let flag = Math.random() > 0.5
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(1)
      if (flag) {
        console.log(num, 11111111111)
        resolve(num)
      } else {
        reject(num)
      }
    }, 2000)
  })
}

let promise = [text(1), text(45), text(2), text(123), text(998), text(998), text(998), text(998), text(998), text(998)]
// let promise = [text, text, text, text, text]
let newD = new Date().getTime()
// sendRequest(promise, 2, value => {
//   console.log(value)
//   console.log(new Date().getTime() - newD)
// })

function _const (key, value) {
  Object.defineProperty(window, key, {
    enumerable: false,
    configurable: false,
    get: function () {
      return value
    },
    set (data) {
      if (data !== value) {
        throw new TypeError('error')
      } else {
        return value
      }
    }
  })
}

_const('aaa', 123)
console.log(window)
aaa = 99
console.log(aaa)
